<template>
    <div class="information">
        <li>资讯管理</li>
        <el-card class="box-card">
            <div class="search">
                <div class="informationSearch">
                    <span>搜索:</span>
                    <el-input placeholder="请输入内容" v-model="keyword"></el-input>
                    <el-button type="primary" @click="handleSearch" >搜索</el-button>
                </div>
                <div class="informationType">
                    <span>状态:</span>
                    <el-select v-model="value" clearable placeholder="请选择" @change="changeSelect" >
                        <el-option label="开启" value="开启"></el-option>
                        <el-option label="关闭" value="关闭"></el-option>
                    </el-select>
                </div>
                <el-button type="primary" @click="addInformation" >添加</el-button>
            </div>
            <el-table
                :data="informationList"
                stripe
                border >
                <el-table-column type="index" label="序号" width="80" ></el-table-column>
                <el-table-column label="文章标题" prop="title" ></el-table-column>
                <el-table-column label="创建人" prop="name" ></el-table-column>
                <el-table-column label="创建日期" prop="time" ></el-table-column>
                <el-table-column label="状态" prop="state" >
                    <template slot-scope="scope">
                        <el-switch
                          v-if="scope.row.state === '开启'"
                          :value="true"
                          active-color="#13ce66"
                          inactive-color="#ff4949">
                        </el-switch>
                        <el-switch
                          v-else
                          :value="false"
                          active-color="#13ce66"
                          inactive-color="#ff4949">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" @click="editInformation(scope.row)" >编辑</el-button>
                        <el-button type="text" @click="deleteInformation(scope.row)" >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.pageIndex"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="page.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
            <el-dialog title="" :visible.sync="dialogFormVisible">
              <el-form :model="form">
                  <el-form-item label="文章标题" :label-width="formLabelWidth">
                      <el-input v-model="form.title" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="创建人" :label-width="formLabelWidth">
                      <el-input v-model="form.name" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="创建日期" :label-width="formLabelWidth">
                      <el-input v-model="form.time" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="状态" :label-width="formLabelWidth">
                      <el-input v-model="form.state" autocomplete="off"></el-input>
                  </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                  <el-button @click="()=>{
                        dialogFormVisible = false
                        this.form = {
                          title: '',
                          name: '',
                          time: '',
                          state: ''
                        }
                      }">取 消</el-button>
                  <el-button type="primary" @click="submin">确 定</el-button>
              </div>
          </el-dialog>
        </el-card>
    </div>
</template>

<script>
export default {
  data () {
    return {
      keyword: '',
      value: '',
      informationList: [],
      page: {
        pageIndex: 1,
        pageSize: 5
      },
      total: 0,
      form: {
        title: '',
        name: '',
        time: '',
        state: ''
      },
      dialogFormVisible: false,
      formLabelWidth: '80px'
    }
  },
  created () {
    this.getInformationList()
  },
  methods: {
    getInformationList () {
      this.$api.getInformation(this.page).then(res => {
        console.log(res)
        if (res.data.code === 1) {
          this.informationList = res.data.data
          this.total = res.data.total
          this.$message.success('获取成功')
        }
      })
    },
    handleSizeChange (val) { // 分页
      this.page.pageSize = val
      this.getInformationList()
    },
    handleCurrentChange (val) {
      this.page.pageIndex = val
      this.getInformationList()
    },
    addInformation () { // 添加
      this.dialogFormVisible = true
    },
    editInformation (row) { // 编辑
      this.dialogFormVisible = true
      this.form = row
    },
    submin () {
      if (this.form.id) {
        this.$api.inforEdit(this.form).then(res => {
          if (res.data.code === 1) {
            this.form = {
              title: '',
              name: '',
              time: '',
              state: ''
            }
            this.dialogFormVisible = false
            this.getInformationList()
          }
        })
      } else {
        this.$api.inforAdd(this.form).then(res => {
          if (res.data.code === 1) {
            this.form = {
              title: '',
              name: '',
              time: '',
              state: ''
            }
            this.dialogFormVisible = false
            this.getInformationList()
          }
        })
      }
    },
    deleteInformation (row) { // 删除
      this.$api.inforDelete({ id: row.id }).then(res => {
        if (res.data.code === 1) {
          this.$message.success('删除成功')
          this.getInformationList()
        }
      })
    },
    handleSearch () {
      this.$api.inforSearch({ title: this.keyword }).then(res => {
        if (res.data.code === 1) {
          this.informationList = res.data.data
        }
      })
    },
    changeSelect () {
      this.$api.inforSelect({ state: this.value }).then(res => {
        if (res.data.code === 1) {
          res.data.data.map(item => {
            if (item.state === '开启') {
              item.state = true
              this.informationList = res.data.data
            } else {
              item.state = false
              this.informationList = res.data.data
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.information{
    width: 100%;
    height: 100%;
}
.box-card{
    margin-top: 10px;
}
.search{
    display: flex;
    align-items: center;
    margin: 10px 0;
}
.informationSearch{
    display: flex;
    align-items: center;
    margin-right: 15px;
}
.informationSearch span{
    width: 60px;
}
.informationType{
    margin-right: 20px;
}
</style>
